<?php
namespace Admin\Controller;
use Think\Controller;
class CommentController extends CommonController {
	public function index(){

		//取出所有的提问还有用户的名称
		$sql = "select t1.*,t2.username as username ,t2.id as userid from question t1 left join user t2 on t1.uid = t2.id";
		$res = M()->query($sql);
		//转换一下时间
		foreach ($res as $key => $value) {
			$res[$key]['create_time'] = date("Y-m-d H:i:s",$res[$key]['create_time']);
		}
		$this->assign('res',$res);
		$this->display();
	}

	//异步评论删除
	public function questiondelete(){
		$questionID = $_POST['question_id'];
		//删除问题 删除问问题之后还要删除对应的回答表的
		$sql = "delete from question where id = {$questionID}";
		if(M()->execute($sql)){
			$sql = "delete from answer where qid = {$questionID}";
			M()->execute($sql);
			echo json_encode(1);
		}
	}

	//问题修改
	public function questionupdate(){
		$qid = $_GET['qid'];
		$sql = "select t1.*,t2.username from question t1 left join user t2 on t1.uid = t2.id where t1.id = {$qid}";
		$res = M()->query($sql);
		//先查询到所有的分类
		$res[0]['cate'] = M()->query("select id,cname from category");
		
		//查询问题已经所选的分类
		$CurrentCateSql = "select cid from category_question where qid = {$qid}";
		$CurrentCateRes = M()->query($CurrentCateSql);
		foreach ($CurrentCateRes as $key => $value) {
			$CurrentCateNewRes[] = $value['cid'];
		}
		$this->assign('CurrentCateRes',$CurrentCateNewRes);
		$this->assign('res',$res);
		$this->display();
	}

	//文本编辑器
    public function ueditor(){
        $data = new \Org\Util\Ueditor();
        echo $data->output();
    }

    //问题保存
    public function editsave(){
    	$data = $_POST;
    	$data['content'] = $_POST['editorValue'];
    	unset($data['editorValue']);
    	$data['create_time'] = strtotime($data['create_time']);
    	if(M('question')->save($data) !== false){
    		//遍历提交过来的分类 想要更新问题分类的表的数据 先删除其原先的分类，然后循环插入
    		$delteCateSql = "delete  from category_question where qid = {$data['id']}";
    		M()->execute($delteCateSql);
    		foreach ($data['cid'] as $key => $value) {
    			$sql ="insert into category_question(cid,qid) values({$value},{$data['id']})";
    			M()->execute($sql);
    		}
    		$this->success('修改成功',U('admin/comment/index'));
    	}else{
    		$this->error('修改失败');
    	}
    }

    //问题增加
    public function questionadd(){
    	//查询所有分类
    	if(IS_GET){
    		$cateRes = M()->query("select id,cname from category");
	    	$this->assign('cateRes',$cateRes);
	    	$this->display();
    	}elseif(IS_POST){
    		//如果是管理员自己添加的 那么就是管理员添加的 user 表 id 为8 的就是管理员
    		$data = $_POST;
    		$data['uid'] = 8;
	    	$data['content'] = $_POST['editorValue'];
	    	unset($data['editorValue']);
	    	$data['create_time'] = strtotime($data['create_time']);
	    	$lastinsertid = M('question')->add($data);
	    	if($lastinsertid){
	    		foreach ($data['cid'] as $key => $value) {
	    			$sql ="insert into category_question(cid,qid) values({$value},{$lastinsertid})";
	    			M()->execute($sql);
	    		}
	    		$this->success('增加成功',U('admin/comment/index'));
	    	}else{
	    		$this->error('增加失败');
	    	}
    	}
    	
    }
}